package com.yds.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

/**
 * 创建者：于德水
 * 创建时间：2021/9/10 0010 11:43
 * 创建描述：头布局
 */

/**
 * Tips:
 * 1.默认样式setIvTopBarLeftBackStyle 和 自定义样式setIvTopBarLeftBackImgResource 不能同时使用
 * 2.默认样式setIvTopBarRightMoreStyle 和 自定义样式setIvTopBarRightMoreImgResource 不能同时使用
 */
@SuppressWarnings("all")
public class YDSTopBar extends LinearLayout {
    private ImageView ivTopBarLeftBack, ivTopBarRightMore, ivTopBarRightMore2;
    private TextView tvTopBarLeftBack, tvTopBarTitle, tvTopBarRightMore;
    private View vSplitLine;
    private OnTopBarClickListener mOnTopBarClickListener;

    private float ivLeftBackPaddingLeft = 0;
    private float ivLeftBackPaddingTop = 0;
    private float ivLeftBackPaddingRight = 0;
    private float ivLeftBackPaddingBottom = 0;

    private float ivRightMorePaddingLeft = 0;
    private float ivRightMorePaddingTop = 0;
    private float ivRightMorePaddingRight = 0;
    private float ivRightMorePaddingBottom = 0;

    private float ivRightMore2PaddingLeft = 0;
    private float ivRightMore2PaddingTop = 0;
    private float ivRightMore2PaddingRight = 0;
    private float ivRightMore2PaddingBottom = 0;

    public YDSTopBar(Context context) {
        this(context, null);
    }

    public YDSTopBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public YDSTopBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        initEmptyView(context, attrs);
    }

    private void initEmptyView(Context context, @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_yds_top_bar, this, true);
        ivTopBarLeftBack = view.findViewById(R.id.ivTopBarLeftBack);
        tvTopBarLeftBack = view.findViewById(R.id.tvTopBarLeftBack);
        tvTopBarTitle = view.findViewById(R.id.tvTopBarTitle);
        tvTopBarRightMore = view.findViewById(R.id.tvTopBarRightMore);
        ivTopBarRightMore = view.findViewById(R.id.ivTopBarRightMore);
        ivTopBarRightMore2 = view.findViewById(R.id.ivTopBarRightMore2);
        vSplitLine = view.findViewById(R.id.vSplitLine);

        ivTopBarLeftBack.setOnClickListener(v -> {
            if (null != mOnTopBarClickListener) {
                mOnTopBarClickListener.onImageViewLeftBackClick();
            }
        });
        tvTopBarLeftBack.setOnClickListener(v -> {
            if (null != mOnTopBarClickListener) {
                mOnTopBarClickListener.onTextViewLeftBackClick();
            }
        });
        tvTopBarRightMore.setOnClickListener(v -> {
            if (null != mOnTopBarClickListener) {
                mOnTopBarClickListener.onTextViewRightMoreClick();
            }
        });
        ivTopBarRightMore.setOnClickListener(v -> {
            if (null != mOnTopBarClickListener) {
                mOnTopBarClickListener.onImageViewRightMoreClick();
            }
        });
        ivTopBarRightMore2.setOnClickListener(v -> {
            if (null != mOnTopBarClickListener) {
                mOnTopBarClickListener.onImageViewRightMore2Click();
            }
        });

        initEmptyTipsAttribute(context, attrs);
    }

    private void initEmptyTipsAttribute(Context context, @org.jetbrains.annotations.Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.YDSTopBar);

        //设置返回imageview 是否可见
        boolean leftBackImgVisible = typedArray.getBoolean(R.styleable.YDSTopBar_topBarLeftBackImgVisible, true);
        setIvTopBarLeftBackVisible(leftBackImgVisible);
        //设置返回imageview 更换样式
        String leftBackImgstyle = typedArray.getString(R.styleable.YDSTopBar_topBarLeftBackImgStyle);
        setIvTopBarLeftBackStyle(leftBackImgstyle);
        //设置返回imageview
        int leftBackImgResourceId = typedArray.getResourceId(R.styleable.YDSTopBar_topBarLeftBackImgResource, 0);
        if (leftBackImgResourceId != 0) {
            setIvTopBarLeftBackImgResource(leftBackImgResourceId);
        }
        //设置返回imageview padding
        float ivLeftBackPadding = typedArray.getFloat(R.styleable.YDSTopBar_topBarLeftBackImgPadding, 13);
        setIvTopBarLeftBackPadding(ivLeftBackPadding, ivLeftBackPadding, ivLeftBackPadding, ivLeftBackPadding);

        ivLeftBackPaddingLeft = typedArray.getFloat(R.styleable.YDSTopBar_topBarLeftBackImgPaddingLeft, 13);
        ivLeftBackPaddingTop = typedArray.getFloat(R.styleable.YDSTopBar_topBarLeftBackImgPaddingTop, 13);
        ivLeftBackPaddingRight = typedArray.getFloat(R.styleable.YDSTopBar_topBarLeftBackImgPaddingRight, 13);
        ivLeftBackPaddingBottom = typedArray.getFloat(R.styleable.YDSTopBar_topBarLeftBackImgPaddingBottom, 13);
        if (ivLeftBackPadding == 0) {
            setIvTopBarLeftBackPadding(ivLeftBackPaddingLeft, ivLeftBackPaddingTop, ivLeftBackPaddingRight, ivLeftBackPaddingBottom);
        }

        //设置返回TextView 是否可见
        boolean leftBackTxtVisible = typedArray.getBoolean(R.styleable.YDSTopBar_topBarLeftBackTxtVisible, false);
        setTvTopBarLeftBackVisible(leftBackTxtVisible);
        //设置返回TextView
        String leftBackTxtContent = typedArray.getString(R.styleable.YDSTopBar_topBarLeftBackTxtContent);
        setTvTopBarLeftBackContent(leftBackTxtContent);
        //设置返回TextView字体样式
        String leftBackTxtStyle = typedArray.getString(R.styleable.YDSTopBar_topBarLeftBackTxtStyle);
        setTvTopBarLeftBackStyle(leftBackTxtStyle);
        //设置返回TextView字体大小
        float leftBackTxtSize = typedArray.getFloat(R.styleable.YDSTopBar_topBarLeftBackTxtSize, 14);
        setTvTopBarLeftBackSize(leftBackTxtSize);
        //设置返回TextView字体颜色
        int leftBackTxtColor = typedArray.getResourceId(R.styleable.YDSTopBar_topBarLeftBackTxtColor, R.color.black);
        setTvTopBarLeftBackColor(leftBackTxtColor);

        //设置标题 是否可见
        boolean titleVisible = typedArray.getBoolean(R.styleable.YDSTopBar_topBarTitleVisible, true);
        setTvTopBarTitleVisible(titleVisible);
        //设置标题
        String titleContent = typedArray.getString(R.styleable.YDSTopBar_topBarTitle);
        setTvTopBarTitle(titleContent);
        //设置标题字体样式
        String titleStyle = typedArray.getString(R.styleable.YDSTopBar_topBarTitleStyle);
        setTvTopBarTitleStyle(titleStyle);
        //设置标题字体大小
        float titleSize = typedArray.getFloat(R.styleable.YDSTopBar_topBarTitleSize, 16);
        setTvTopBarTitleSize(titleSize);
        //设置标题字体颜色
        int titleColor = typedArray.getResourceId(R.styleable.YDSTopBar_topBarTitleColor, R.color.black);
        setTvTopBarTitleColor(titleColor);

        //设置更多TextView 是否可见
        boolean rightMoreTxtVisible = typedArray.getBoolean(R.styleable.YDSTopBar_topBarRightMoreTxtVisible, false);
        setTvTopBarRightMoreVisible(rightMoreTxtVisible);
        //设置更多TextView
        String rightMoreTxtContent = typedArray.getString(R.styleable.YDSTopBar_topBarRightMoreTxtContent);
        setTvTopBarRightMoreContent(rightMoreTxtContent);
        //设置更多TextView字体样式
        String rightMoreTxtStyle = typedArray.getString(R.styleable.YDSTopBar_topBarRightMoreTxtStyle);
        setTvTopBarRightMoreStyle(rightMoreTxtStyle);
        //设置更多TextView字体大小
        float rightMoreTxtSize = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMoreTxtSize, 14);
        setTvTopBarRightMoreSize(rightMoreTxtSize);
        //设置更多TextView字体颜色
        int rightMoreTxtColor = typedArray.getResourceId(R.styleable.YDSTopBar_topBarRightMoreTxtColor, R.color.black);
        setTvTopBarRightMoreColor(rightMoreTxtColor);

        //设置更多imageview 是否可见
        boolean rightMoreImgVisible = typedArray.getBoolean(R.styleable.YDSTopBar_topBarRightMoreImgVisible, true);
        setIvTopBarRightMoreVisible(rightMoreImgVisible);
        //设置更多imageview 更换样式
        String rightMoreImgstyle = typedArray.getString(R.styleable.YDSTopBar_topBarRightMoreImgStyle);
        setIvTopBarRightMoreStyle(rightMoreImgstyle);
        //设置更多imageview
        int rightMoreImgResourceId = typedArray.getResourceId(R.styleable.YDSTopBar_topBarRightMoreImgResource, 0);
        if (rightMoreImgResourceId != 0) {
            setIvTopBarRightMoreImgResource(rightMoreImgResourceId);
        }
        //设置更多imageview padding
        float ivRightMorePadding = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMoreImgPadding, 0);
        setIvTopBarRightMorePadding(ivRightMorePadding, ivRightMorePadding, ivRightMorePadding, ivRightMorePadding);

        ivRightMorePaddingLeft = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMoreImgPaddingLeft, 13);
        ivRightMorePaddingTop = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMoreImgPaddingTop, 13);
        ivRightMorePaddingRight = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMoreImgPaddingRight, 13);
        ivRightMorePaddingBottom = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMoreImgPaddingBottom, 13);
        if (ivRightMorePadding == 0) {
            setIvTopBarRightMorePadding(ivRightMorePaddingLeft, ivRightMorePaddingTop, ivRightMorePaddingRight, ivRightMorePaddingBottom);
        }

        //设置更多2imageview 是否可见
        boolean rightMore2ImgVisible = typedArray.getBoolean(R.styleable.YDSTopBar_topBarRightMore2ImgVisible, false);
        setIvTopBarRightMore2Visible(rightMore2ImgVisible);
        //设置更多2imageview
        int rightMore2ImgResourceId = typedArray.getResourceId(R.styleable.YDSTopBar_topBarRightMore2ImgResource, 0);
        if (rightMore2ImgResourceId != 0) {
            setIvTopBarRightMore2ImgResource(rightMore2ImgResourceId);
        }
        //设置更多2imageview padding
        float ivRightMore2Padding = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMore2ImgPadding, 0);
        setIvTopBarRightMore2Padding(ivRightMore2Padding, ivRightMore2Padding, ivRightMore2Padding, ivRightMore2Padding);

        ivRightMore2PaddingLeft = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMore2ImgPaddingLeft, 13);
        ivRightMore2PaddingTop = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMore2ImgPaddingTop, 13);
        ivRightMore2PaddingRight = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMore2ImgPaddingRight, 13);
        ivRightMore2PaddingBottom = typedArray.getFloat(R.styleable.YDSTopBar_topBarRightMore2ImgPaddingBottom, 13);
        if (ivRightMore2Padding == 0) {
            setIvTopBarRightMore2Padding(ivRightMore2PaddingLeft, ivRightMore2PaddingTop, ivRightMore2PaddingRight, ivRightMore2PaddingBottom);
        }

        //设置分割线View 是否可见
        boolean vSplitLineVisible = typedArray.getBoolean(R.styleable.YDSTopBar_topBarSplitLineVisible, false);
        setVSplitLineVisible(vSplitLineVisible);
        //设置分割线View 背景色
        int vSplitLineColor = typedArray.getResourceId(R.styleable.YDSTopBar_topBarSplitLineBackground, R.color.black);
        setVSplitLineBackground(vSplitLineColor);
    }

    /**
     * 设置返回imageview 是否可见
     *
     * @param visible
     */
    public void setIvTopBarLeftBackVisible(boolean visible) {
        ivTopBarLeftBack.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置返回imageview 更换样式
     *
     * @param style - android or ios or normal
     */
    public void setIvTopBarLeftBackStyle(String style) {
        if (TextUtils.isEmpty(style)) return;
        if (style.equals("android")) {
            setIvTopBarLeftBackImgResource(R.drawable.ic_baseline_arrow_back_android);
        } else if (style.equals("ios")) {
            setIvTopBarLeftBackImgResource(R.mipmap.icon_left_back_ios);
        } else if (style.equals("normal")) {
            setIvTopBarLeftBackImgResource(R.mipmap.icon_left_back_normal);
        }
    }

    /**
     * 设置返回imageview
     *
     * @param resourceId
     */
    public void setIvTopBarLeftBackImgResource(int resourceId) {
        ivTopBarLeftBack.setImageResource(resourceId);
    }

    /**
     * 设置返回imageview padding
     *
     * @param paddingLeft
     * @param paddingTop
     * @param paddingRight
     * @param paddingBottom
     */
    public void setIvTopBarLeftBackPadding(float paddingLeft, float paddingTop, float paddingRight, float paddingBottom) {
        ivTopBarLeftBack.setPadding(dpToPx(paddingLeft), dpToPx(paddingTop), dpToPx(paddingRight), dpToPx(paddingBottom));
    }

    /**
     * 设置返回TextView 是否可见
     *
     * @param visible
     */
    public void setTvTopBarLeftBackVisible(boolean visible) {
        tvTopBarLeftBack.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置返回TextView
     *
     * @param content
     */
    public void setTvTopBarLeftBackContent(String content) {
        if (TextUtils.isEmpty(content)) return;
        tvTopBarLeftBack.setText(content);
    }

    /**
     * 设置返回TextView字体样式
     *
     * @param type - normal or bold
     */
    public void setTvTopBarLeftBackStyle(String type) {
        if (TextUtils.isEmpty(type)) return;
        if (type.equals("normal")) {
            tvTopBarLeftBack.setTypeface(Typeface.DEFAULT);
        } else if (type.equals("bold")) {
            tvTopBarLeftBack.setTypeface(Typeface.DEFAULT_BOLD);
        }
    }

    /**
     * 设置返回TextView字体大小
     *
     * @param sp 默认单位 sp
     */
    public void setTvTopBarLeftBackSize(float sp) {
        tvTopBarLeftBack.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
    }

    /**
     * 设置返回TextView字体颜色
     *
     * @param sp 默认单位 sp
     */
    public void setTvTopBarLeftBackColor(int colorId) {
        tvTopBarLeftBack.setTextColor(getContext().getResources().getColor(colorId));
    }

    /**
     * 设置标题 是否可见
     *
     * @param visible
     */
    public void setTvTopBarTitleVisible(boolean visible) {
        tvTopBarTitle.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置标题
     *
     * @param content
     */
    public void setTvTopBarTitle(String title) {
        if (TextUtils.isEmpty(title)) return;
        tvTopBarTitle.setText(title);
    }

    /**
     * 设置标题 字体样式
     *
     * @param type - normal or bold
     */
    public void setTvTopBarTitleStyle(String type) {
        if (TextUtils.isEmpty(type)) return;
        if (type.equals("normal")) {
            tvTopBarTitle.setTypeface(Typeface.DEFAULT);
        } else if (type.equals("bold")) {
            tvTopBarTitle.setTypeface(Typeface.DEFAULT_BOLD);
        }
    }

    /**
     * 设置标题 字体大小
     *
     * @param sp 默认单位 sp
     */
    public void setTvTopBarTitleSize(float sp) {
        tvTopBarTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
    }

    /**
     * 设置标题 字体颜色
     *
     * @param sp 默认单位 sp
     */
    public void setTvTopBarTitleColor(int colorId) {
        tvTopBarTitle.setTextColor(getContext().getResources().getColor(colorId));
    }

    /**
     * 设置更多TextView 是否可见
     *
     * @param visible
     */
    public void setTvTopBarRightMoreVisible(boolean visible) {
        tvTopBarRightMore.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置更多TextView
     *
     * @param content
     */
    public void setTvTopBarRightMoreContent(String content) {
        if (TextUtils.isEmpty(content)) return;
        tvTopBarRightMore.setText(content);
    }

    /**
     * 设置更多TextView字体样式
     *
     * @param type - normal or bold
     */
    public void setTvTopBarRightMoreStyle(String type) {
        if (TextUtils.isEmpty(type)) return;
        if (type.equals("normal")) {
            tvTopBarRightMore.setTypeface(Typeface.DEFAULT);
        } else if (type.equals("bold")) {
            tvTopBarRightMore.setTypeface(Typeface.DEFAULT_BOLD);
        }
    }

    /**
     * 设置更多TextView字体大小
     *
     * @param sp 默认单位 sp
     */
    public void setTvTopBarRightMoreSize(float sp) {
        tvTopBarRightMore.setTextSize(TypedValue.COMPLEX_UNIT_SP, sp);
    }

    /**
     * 设置更多TextView字体颜色
     *
     * @param sp 默认单位 sp
     */
    public void setTvTopBarRightMoreColor(int colorId) {
        tvTopBarRightMore.setTextColor(getContext().getResources().getColor(colorId));
    }

    /**
     * 设置更多imageview 是否可见
     *
     * @param visible
     */
    public void setIvTopBarRightMoreVisible(boolean visible) {
        ivTopBarRightMore.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置更多imageview 更换样式
     *
     * @param style - dot_vertical or dot_horizontal or setting
     */
    public void setIvTopBarRightMoreStyle(String style) {
        if (TextUtils.isEmpty(style)) return;
        if (style.equals("dot_horizontal")) {
            setIvTopBarRightMoreImgResource(R.mipmap.icon_right_more_horiz);
        } else if (style.equals("dot_vertical")) {
            setIvTopBarRightMoreImgResource(R.drawable.ic_baseline_more_vert);
        } else if (style.equals("setting")) {
            setIvTopBarRightMoreImgResource(R.drawable.ic_baseline_settings);
        } else if (style.equals("add")) {
            setIvTopBarRightMoreImgResource(R.mipmap.icon_right_more_add);
        }
        setIvTopBarRightMorePadding(ivRightMorePaddingLeft, ivRightMorePaddingTop, ivRightMorePaddingRight, ivRightMorePaddingBottom);
    }

    /**
     * 设置更多imageview
     *
     * @param resourceId
     */
    public void setIvTopBarRightMoreImgResource(int resourceId) {
        ivTopBarRightMore.setImageResource(resourceId);
    }

    /**
     * 设置更多imageview padding
     *
     * @param paddingLeft
     * @param paddingTop
     * @param paddingRight
     * @param paddingBottom
     */
    public void setIvTopBarRightMorePadding(float paddingLeft, float paddingTop, float paddingRight, float paddingBottom) {
        ivTopBarRightMore.setPadding(dpToPx(paddingLeft), dpToPx(paddingTop), dpToPx(paddingRight), dpToPx(paddingBottom));
    }

    /**
     * 设置更多2imageview 是否可见
     *
     * @param visible
     */
    public void setIvTopBarRightMore2Visible(boolean visible) {
        ivTopBarRightMore2.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置更多2imageview
     *
     * @param resourceId
     */
    public void setIvTopBarRightMore2ImgResource(int resourceId) {
        ivTopBarRightMore2.setImageResource(resourceId);
    }

    /**
     * 设置更多2imageview padding
     *
     * @param paddingLeft
     * @param paddingTop
     * @param paddingRight
     * @param paddingBottom
     */
    public void setIvTopBarRightMore2Padding(float paddingLeft, float paddingTop, float paddingRight, float paddingBottom) {
        ivTopBarRightMore2.setPadding(dpToPx(paddingLeft), dpToPx(paddingTop), dpToPx(paddingRight), dpToPx(paddingBottom));
    }

    /**
     * 设置分割线View 是否可见
     *
     * @param visible
     */
    public void setVSplitLineVisible(boolean visible) {
        vSplitLine.setVisibility(visible ? VISIBLE : GONE);
    }

    /**
     * 设置分割线View 背景色
     *
     * @param bgId
     */
    public void setVSplitLineBackground(int bgId) {
        vSplitLine.setBackgroundColor(getResources().getColor(bgId));
    }

    /**
     * 设置TopBar点击事件回调
     *
     * @param clickCallBack
     */
    public void setOnTopBarClickListener(OnTopBarClickListener listener) {
        mOnTopBarClickListener = listener;
    }

    /**
     * TopBar点击事件回调
     */
    public interface OnTopBarClickListener {
        void onImageViewLeftBackClick();

        default void onTextViewLeftBackClick() {
        }

        default void onTextViewRightMoreClick() {
        }

        default void onImageViewRightMoreClick() {
        }

        default void onImageViewRightMore2Click() {
        }
    }

    /**
     * dp to px
     */
    public int dpToPx(float dp) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5);
    }
}
